package com.ruoyi.project.web.tRoominfo.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.web.tRoomBill.domain.TRoomBill;
import com.ruoyi.project.web.tRoomBill.service.ITRoomBillService;
import com.ruoyi.project.web.tRoominfo.domain.TRoominfo;
import com.ruoyi.project.web.tRoominfo.service.ITRoominfoService;

/**
 * 房间信息操作处理
 * 
 * @author ruoyi
 * @date 2021-07-06
 */
@Controller
@RequestMapping("/web/tRoominfo")
public class TRoominfoController extends BaseController
{
    private String prefix = "web/tRoominfo";
    
    @Autowired
	private ITRoomBillService tRoomBillService;
	
	@Autowired
	private ITRoominfoService tRoominfoService;
	
	@RequiresPermissions("web:tRoominfo:view")
	@GetMapping()
	public String tRoominfo()
	{
	    return prefix + "/tRoominfo";
	}
	
	/**
	 * 查询房间列表
	 */
	@RequiresPermissions("web:tRoominfo:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(TRoominfo tRoominfo)
	{
		startPage();
		tRoominfo.setFjh("");
        List<TRoominfo> list = tRoominfoService.selectTRoominfoList(tRoominfo);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出房间列表
	 */
	@RequiresPermissions("web:tRoominfo:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TRoominfo tRoominfo)
    {
    	List<TRoominfo> list = tRoominfoService.selectTRoominfoList(tRoominfo);
        ExcelUtil<TRoominfo> util = new ExcelUtil<TRoominfo>(TRoominfo.class);
        return util.exportExcel(list, "tRoominfo");
    }
	
	/**
	 * 新增房间
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存房间
	 */
	@RequiresPermissions("web:tRoominfo:add")
	@Log(title = "房间", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(TRoominfo tRoominfo)
	{		
		tRoominfo.setId(UUID.randomUUID().toString());
		return toAjax(tRoominfoService.insertTRoominfo(tRoominfo));
	}

	/**
	 * 修改房间
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap)
	{
		TRoominfo tRoominfo = tRoominfoService.selectTRoominfoById(id);
		mmap.put("tRoominfo", tRoominfo);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存房间
	 */
	@RequiresPermissions("web:tRoominfo:edit")
	@Log(title = "房间", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(TRoominfo tRoominfo)
	{		
		
		if("预约".equals(tRoominfo.getZt())){
			tRoominfo.setKssj(null);
		}else if("空闲".equals(tRoominfo.getZt())){
			tRoominfo.setKssj(null);
		}
		
		return toAjax(tRoominfoService.updateTRoominfo(tRoominfo));
	}
	
	/**
	 * 更新房间信息
	 */
	@Log(title = "房间", businessType = BusinessType.UPDATE)
	@PostMapping("/updateRoom")
	@ResponseBody
	public AjaxResult updateRoom(TRoominfo tRoominfo)
	{		
		
		if("在线".equals(tRoominfo.getZt())){
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
			String formatDate = dateFormat.format(new Date());
			tRoominfo.setKssj(formatDate);
			
			//插入房间流水
			TRoominfo tRoominfoLog = tRoominfoService.selectTRoominfoById(tRoominfo.getId());
			
			TRoomBill tRoomBill = new TRoomBill();
			tRoomBill.setId(UUID.randomUUID().toString());
			tRoomBill.setZt("在线");
			tRoomBill.setFjh(tRoominfoLog.getFjh());
			tRoomBill.setKssj(formatDate);
			tRoomBill.setJcfy(tRoominfoLog.getJcff());
			tRoomBill.setTxr(ShiroUtils.getSysUser().getUserId()+"");
			tRoomBill.setTxsj(formatDate);
			tRoomBill.setZfy(tRoominfoLog.getJcff());
			
			tRoomBillService.insertTRoomBill(tRoomBill);
			
		}
		return toAjax(tRoominfoService.updateTRoominfo(tRoominfo));
	}
	
	/**
	 * 更新房间信息
	 */
	@RequiresPermissions("web:tRoominfo:updateRoom")
	@Log(title = "房间", businessType = BusinessType.UPDATE)
	@PostMapping("/updateRoomZt")
	@ResponseBody
	public AjaxResult updateRoomZt(TRoominfo tRoominfo)
	{		
		
		List<TRoominfo> tRoominfoList = tRoominfoService.selectTRoominfoList(tRoominfo);
		tRoominfo = tRoominfoList.get(0);
		tRoominfo.setZt("空闲");
		tRoominfo.setKssj(null);
		
		return toAjax(tRoominfoService.updateTRoominfo(tRoominfo));
	}
	
	/**
	 * 删除房间
	 */
	@RequiresPermissions("web:tRoominfo:remove")
	@Log(title = "房间", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(tRoominfoService.deleteTRoominfoByIds(ids));
	}
	
}
